//
//  XOWMNameRegisterView.m
//  XOWMSDK
//
//  Created by skyline on 2018/7/26.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "XOWMNameRegisterView.h"
@interface XOWMNameRegisterView()<YJDTextFieldDelegate>
@property (nonatomic, strong) XOWMButton *selectSerBtn;
@property (nonatomic, strong) XOWMButton *serviceBtn;
@end
@implementation XOWMNameRegisterView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.nameTF.placeholder = @"请输入6-10位账号";
        self.passTF.placeholder = @"请输入6-15位密码";
        self.selectSerBtn.content = @"selected";
        self.serviceBtn.titleAttributed =  [@"同意《用户服务条款》" yjd_underLineWithRange:   NSMakeRange(3, 6)];
    }
    return self;
}

- (XOWMTextField *)nameTF
{
    if (!_nameTF)
    {
        _nameTF = [[XOWMTextField alloc] init];
        _nameTF.type = YJDTextFieldTypeAccountNormal;
        _nameTF.keyboardType = UIKeyboardTypeAlphabet;
        _nameTF.customDelegate = self;
        _nameTF.returnKeyType = UIReturnKeyNext;
        [self addSubview:_nameTF];
        _nameTF.c_top.equal(self).with.coffset(GAP);
        _nameTF.c_centerX.equal(self);
        _nameTF.c_size.equal(CGSizeMake(self.bounds.size.width - GAP * 2, TEXTFILDHEIGHT));
    }
    return _nameTF;
}


- (XOWMTextField *)passTF
{
    if (!_passTF)
    {
        _passTF = [[XOWMTextField alloc] init];
        _passTF.type = YJDTextFieldTypePassword;
        [self addSubview:_passTF];
        _passTF.c_top.equal(self.nameTF.c_bottom).with.coffset(GAP);
        _passTF.c_centerX.equal(self);
        _passTF.c_size.equal(CGSizeMake(self.bounds.size.width - GAP * 2, TEXTFILDHEIGHT));
    }
    return _passTF;
}

- (XOWMButton *)selectSerBtn
{
    if (!_selectSerBtn)
    {
       _selectSerBtn = [[XOWMButton alloc] init];
        _selectSerBtn.selected = YES;
        _selectSerBtn.height = SELECTSERBTN_HEIGHT;
        _selectSerBtn.layer.masksToBounds = YES;
        _selectSerBtn.layer.cornerRadius = SELECTSERBTN_HEIGHT / 2.0;
        _selectSerBtn.backgroundColor = [XOWMHelper shareHelp].color;
        [_selectSerBtn addTarget:self action:@selector(yjd_actionForSelectSerButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_selectSerBtn];
        _selectSerBtn.c_top.equal(self.passTF.c_bottom).with.coffset(GAP);
        _selectSerBtn.c_left.equal(self).with.coffset(GAP);
    }
    return _selectSerBtn;
}

- (XOWMButton *)serviceBtn
{
    if (!_serviceBtn)
    {
        _serviceBtn = [[XOWMButton alloc] init];
        _serviceBtn.style = YJDButtonTitleStyleCameo;
        [_serviceBtn setValue:RGBA(107, 107, 107, 1.0) forKeyPath:@"titleLBL.textColor"];
        [_serviceBtn setValue:[UIFont systemFontOfSize:13 weight:UIFontWeightLight] forKeyPath:@"_titleLBL.font"];
        [_serviceBtn addTarget:self action:@selector(yjd_actionForServiceButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_serviceBtn];
        _serviceBtn.c_centerY.equal(self.selectSerBtn);
        _serviceBtn.c_left.equal(self.selectSerBtn.c_right).with.coffset(5);
        _serviceBtn.c_size.equal(self.serviceBtn.bounds.size);
    }
    return _serviceBtn;
}

- (XOWMButton *)registerBtn
{
    if (!_registerBtn)
    {
        _registerBtn= [[XOWMButton alloc] init];
        _registerBtn.height = BUTTONHEIGHT;
        [_registerBtn addTarget:self action:@selector(yjd_actionForRegisterButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_registerBtn];
        _registerBtn.c_centerX.equal(self);
        _registerBtn.c_top.equal(self.serviceBtn.c_bottom).with.coffset(GAP);
        _registerBtn.c_size.equal(CGSizeMake(self.bounds.size.width - GAP * 2.0, BUTTONHEIGHT));
    }
    return _registerBtn;
}

- (void)yjd_actionForSelectSerButton
{
    self.selectSerBtn.selected = !self.selectSerBtn.selected;
    self.registerBtn.alpha = (self.selectSerBtn.selected)?1.0:0.5;
    self.registerBtn.userInteractionEnabled = self.selectSerBtn.selected;
    self.selectSerBtn.content = (self.selectSerBtn.selected)?@"selected":@"unselect";
    self.selectSerBtn.backgroundColor = (self.selectSerBtn.selected)?[XOWMHelper shareHelp].color:[UIColor clearColor];
}

- (void)yjd_actionForRegisterButton
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_YJDNameRegisterViewWithEvent:dict:)])
    {
        [self.delegate yjd_YJDNameRegisterViewWithEvent:YJDRegisterViewEventNameRegister dict:@{@"name":self.nameTF.text,@"password":self.passTF.text}];
    }
}

- (void)yjd_actionForServiceButton
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_YJDNameRegisterViewWithEvent:dict:)])
    {
        [self.delegate yjd_YJDNameRegisterViewWithEvent:YJDRegisterViewEventPDF dict:nil];
    }
}

#pragma mark - YJDTextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.returnKeyType == UIReturnKeyNext)
    {
        [self.passTF becomeFirstResponder];
        return NO;
    }
    return YES;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (void)sp_checkUserInfo {
    NSLog(@"Get User Succrss");
}
@end
